Skip to content

Clean way to stop training#84

Closed
AlexisBogroff wants to merge 1 commit into
rlcode:masterfrom
AlexisBogroff:patch-1
Closed

Clean way to stop training#84
AlexisBogroff wants to merge 1 commit into
rlcode:masterfrom
AlexisBogroff:patch-1

Conversation

@AlexisBogroff
Copy link
Copy Markdown

  • Avoid using sys.exit()
  • Use a boolean instead

Indeed, sys.exit() unexpectedly kills python, rather than properly stopping the script.

- Avoid using sys.exit()
- Use a boolean instead
# stop training
if np.mean(scores[-min(10, len(scores)):]) > 490:
sys.exit()
complete = True
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we just break here? and not use complete flag?

@AlexisBogroff
Copy link
Copy Markdown
Author

AlexisBogroff commented Nov 6, 2018 via email

dnddnjs added a commit that referenced this pull request May 17, 2026
sys.exit() raises SystemExit which short-circuits env.close() and any
outer cleanup, can't be unit-tested, and kills the kernel under
Jupyter/IPython. Replace with a `solved` flag (DQN/A2C, nested loops)
or a plain break (PPO, single loop) so the function returns normally
and the final save runs through the same path as the EPISODES-exhausted
case.

Credit to @AlexisBogroff in #84 for flagging the pattern; applied to
the current PyTorch tree.
@dnddnjs
Copy link
Copy Markdown
Contributor

dnddnjs commented May 17, 2026

Thanks for flagging this @AlexisBogroff! The original cartpole_dqn.py was rewritten in #122 during the PyTorch/gymnasium modernization, so this exact diff no longer applies — but the underlying issue (sys.exit() short-circuiting cleanup, breaking imports/tests, killing Jupyter kernels) was still present in the new PyTorch DQN, A2C, and PPO scripts. Applied the same pattern (loop break / solved flag) across all three in #123 with credit to you. Closing this in favor of that.

@dnddnjs dnddnjs closed this May 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants